package com.ugoodtech.mdcc.core.model.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.ugoodtech.mdcc.core.model.entity.UserProfiles;
import com.ugoodtech.mdcc.core.model.entity.UserProfilesExample.Criteria;
import com.ugoodtech.mdcc.core.model.entity.UserProfilesExample.Criterion;
import com.ugoodtech.mdcc.core.model.entity.UserProfilesExample;
import java.util.List;
import java.util.Map;

public class UserProfilesSqlProvider {

    public String countByExample(UserProfilesExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("user_profiles");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(UserProfilesExample example) {
        BEGIN();
        DELETE_FROM("user_profiles");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(UserProfiles record) {
        BEGIN();
        INSERT_INTO("user_profiles");
        
        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            VALUES("creation_time", "#{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            VALUES("deleted", "#{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            VALUES("enabled", "#{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAvatar() != null) {
            VALUES("avatar", "#{avatar,jdbcType=VARCHAR}");
        }
        
        if (record.getCompany() != null) {
            VALUES("company", "#{company,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            VALUES("email", "#{email,jdbcType=VARCHAR}");
        }
        
        if (record.getGender() != null) {
            VALUES("gender", "#{gender,jdbcType=INTEGER}");
        }
        
        if (record.getLevel() != null) {
            VALUES("level", "#{level,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilephone() != null) {
            VALUES("mobilePhone", "#{mobilephone,jdbcType=VARCHAR}");
        }
        
        if (record.getNickname() != null) {
            VALUES("nickname", "#{nickname,jdbcType=VARCHAR}");
        }
        
        if (record.getPosition() != null) {
            VALUES("position", "#{position,jdbcType=VARCHAR}");
        }
        
        if (record.getRealname() != null) {
            VALUES("realName", "#{realname,jdbcType=VARCHAR}");
        }
        
        if (record.getSerialnumber() != null) {
            VALUES("serialNumber", "#{serialnumber,jdbcType=VARCHAR}");
        }
        
        if (record.getTelephone() != null) {
            VALUES("telephone", "#{telephone,jdbcType=VARCHAR}");
        }
        
        if (record.getAddProfileId() != null) {
            VALUES("add_profile_id", "#{addProfileId,jdbcType=BIGINT}");
        }
        
        if (record.getDeparmentId() != null) {
            VALUES("deparment_id", "#{deparmentId,jdbcType=BIGINT}");
        }
        
        if (record.getParentSaleId() != null) {
            VALUES("parent_sale_id", "#{parentSaleId,jdbcType=BIGINT}");
        }
        
        if (record.getTechnologyDeparmentId() != null) {
            VALUES("technology_deparment_id", "#{technologyDeparmentId,jdbcType=BIGINT}");
        }
        
        if (record.getTechnologyProfileId() != null) {
            VALUES("technology_profile_id", "#{technologyProfileId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }
        
        if (record.getIsAdmin() != null) {
            VALUES("is_admin", "#{isAdmin,jdbcType=BIT}");
        }
        
        if (record.getProinceBrand() != null) {
            VALUES("proince_brand", "#{proinceBrand,jdbcType=LONGVARCHAR}");
        }
        
        return SQL();
    }

    public String selectByExampleWithBLOBs(UserProfilesExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("creation_time");
        SELECT("deleted");
        SELECT("enabled");
        SELECT("update_time");
        SELECT("avatar");
        SELECT("company");
        SELECT("email");
        SELECT("gender");
        SELECT("level");
        SELECT("mobilePhone");
        SELECT("nickname");
        SELECT("position");
        SELECT("realName");
        SELECT("serialNumber");
        SELECT("telephone");
        SELECT("add_profile_id");
        SELECT("deparment_id");
        SELECT("parent_sale_id");
        SELECT("technology_deparment_id");
        SELECT("technology_profile_id");
        SELECT("user_id");
        SELECT("is_admin");
        SELECT("proince_brand");
        FROM("user_profiles");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String selectByExample(UserProfilesExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("creation_time");
        SELECT("deleted");
        SELECT("enabled");
        SELECT("update_time");
        SELECT("avatar");
        SELECT("company");
        SELECT("email");
        SELECT("gender");
        SELECT("level");
        SELECT("mobilePhone");
        SELECT("nickname");
        SELECT("position");
        SELECT("realName");
        SELECT("serialNumber");
        SELECT("telephone");
        SELECT("add_profile_id");
        SELECT("deparment_id");
        SELECT("parent_sale_id");
        SELECT("technology_deparment_id");
        SELECT("technology_profile_id");
        SELECT("user_id");
        SELECT("is_admin");
        FROM("user_profiles");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        UserProfiles record = (UserProfiles) parameter.get("record");
        UserProfilesExample example = (UserProfilesExample) parameter.get("example");
        
        BEGIN();
        UPDATE("user_profiles");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{record.deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{record.enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAvatar() != null) {
            SET("avatar = #{record.avatar,jdbcType=VARCHAR}");
        }
        
        if (record.getCompany() != null) {
            SET("company = #{record.company,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            SET("email = #{record.email,jdbcType=VARCHAR}");
        }
        
        if (record.getGender() != null) {
            SET("gender = #{record.gender,jdbcType=INTEGER}");
        }
        
        if (record.getLevel() != null) {
            SET("level = #{record.level,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilephone() != null) {
            SET("mobilePhone = #{record.mobilephone,jdbcType=VARCHAR}");
        }
        
        if (record.getNickname() != null) {
            SET("nickname = #{record.nickname,jdbcType=VARCHAR}");
        }
        
        if (record.getPosition() != null) {
            SET("position = #{record.position,jdbcType=VARCHAR}");
        }
        
        if (record.getRealname() != null) {
            SET("realName = #{record.realname,jdbcType=VARCHAR}");
        }
        
        if (record.getSerialnumber() != null) {
            SET("serialNumber = #{record.serialnumber,jdbcType=VARCHAR}");
        }
        
        if (record.getTelephone() != null) {
            SET("telephone = #{record.telephone,jdbcType=VARCHAR}");
        }
        
        if (record.getAddProfileId() != null) {
            SET("add_profile_id = #{record.addProfileId,jdbcType=BIGINT}");
        }
        
        if (record.getDeparmentId() != null) {
            SET("deparment_id = #{record.deparmentId,jdbcType=BIGINT}");
        }
        
        if (record.getParentSaleId() != null) {
            SET("parent_sale_id = #{record.parentSaleId,jdbcType=BIGINT}");
        }
        
        if (record.getTechnologyDeparmentId() != null) {
            SET("technology_deparment_id = #{record.technologyDeparmentId,jdbcType=BIGINT}");
        }
        
        if (record.getTechnologyProfileId() != null) {
            SET("technology_profile_id = #{record.technologyProfileId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            SET("user_id = #{record.userId,jdbcType=BIGINT}");
        }
        
        if (record.getIsAdmin() != null) {
            SET("is_admin = #{record.isAdmin,jdbcType=BIT}");
        }
        
        if (record.getProinceBrand() != null) {
            SET("proince_brand = #{record.proinceBrand,jdbcType=LONGVARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExampleWithBLOBs(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("user_profiles");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        SET("deleted = #{record.deleted,jdbcType=BIT}");
        SET("enabled = #{record.enabled,jdbcType=BIT}");
        SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        SET("avatar = #{record.avatar,jdbcType=VARCHAR}");
        SET("company = #{record.company,jdbcType=VARCHAR}");
        SET("email = #{record.email,jdbcType=VARCHAR}");
        SET("gender = #{record.gender,jdbcType=INTEGER}");
        SET("level = #{record.level,jdbcType=VARCHAR}");
        SET("mobilePhone = #{record.mobilephone,jdbcType=VARCHAR}");
        SET("nickname = #{record.nickname,jdbcType=VARCHAR}");
        SET("position = #{record.position,jdbcType=VARCHAR}");
        SET("realName = #{record.realname,jdbcType=VARCHAR}");
        SET("serialNumber = #{record.serialnumber,jdbcType=VARCHAR}");
        SET("telephone = #{record.telephone,jdbcType=VARCHAR}");
        SET("add_profile_id = #{record.addProfileId,jdbcType=BIGINT}");
        SET("deparment_id = #{record.deparmentId,jdbcType=BIGINT}");
        SET("parent_sale_id = #{record.parentSaleId,jdbcType=BIGINT}");
        SET("technology_deparment_id = #{record.technologyDeparmentId,jdbcType=BIGINT}");
        SET("technology_profile_id = #{record.technologyProfileId,jdbcType=BIGINT}");
        SET("user_id = #{record.userId,jdbcType=BIGINT}");
        SET("is_admin = #{record.isAdmin,jdbcType=BIT}");
        SET("proince_brand = #{record.proinceBrand,jdbcType=LONGVARCHAR}");
        
        UserProfilesExample example = (UserProfilesExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("user_profiles");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        SET("deleted = #{record.deleted,jdbcType=BIT}");
        SET("enabled = #{record.enabled,jdbcType=BIT}");
        SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        SET("avatar = #{record.avatar,jdbcType=VARCHAR}");
        SET("company = #{record.company,jdbcType=VARCHAR}");
        SET("email = #{record.email,jdbcType=VARCHAR}");
        SET("gender = #{record.gender,jdbcType=INTEGER}");
        SET("level = #{record.level,jdbcType=VARCHAR}");
        SET("mobilePhone = #{record.mobilephone,jdbcType=VARCHAR}");
        SET("nickname = #{record.nickname,jdbcType=VARCHAR}");
        SET("position = #{record.position,jdbcType=VARCHAR}");
        SET("realName = #{record.realname,jdbcType=VARCHAR}");
        SET("serialNumber = #{record.serialnumber,jdbcType=VARCHAR}");
        SET("telephone = #{record.telephone,jdbcType=VARCHAR}");
        SET("add_profile_id = #{record.addProfileId,jdbcType=BIGINT}");
        SET("deparment_id = #{record.deparmentId,jdbcType=BIGINT}");
        SET("parent_sale_id = #{record.parentSaleId,jdbcType=BIGINT}");
        SET("technology_deparment_id = #{record.technologyDeparmentId,jdbcType=BIGINT}");
        SET("technology_profile_id = #{record.technologyProfileId,jdbcType=BIGINT}");
        SET("user_id = #{record.userId,jdbcType=BIGINT}");
        SET("is_admin = #{record.isAdmin,jdbcType=BIT}");
        
        UserProfilesExample example = (UserProfilesExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(UserProfiles record) {
        BEGIN();
        UPDATE("user_profiles");
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAvatar() != null) {
            SET("avatar = #{avatar,jdbcType=VARCHAR}");
        }
        
        if (record.getCompany() != null) {
            SET("company = #{company,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            SET("email = #{email,jdbcType=VARCHAR}");
        }
        
        if (record.getGender() != null) {
            SET("gender = #{gender,jdbcType=INTEGER}");
        }
        
        if (record.getLevel() != null) {
            SET("level = #{level,jdbcType=VARCHAR}");
        }
        
        if (record.getMobilephone() != null) {
            SET("mobilePhone = #{mobilephone,jdbcType=VARCHAR}");
        }
        
        if (record.getNickname() != null) {
            SET("nickname = #{nickname,jdbcType=VARCHAR}");
        }
        
        if (record.getPosition() != null) {
            SET("position = #{position,jdbcType=VARCHAR}");
        }
        
        if (record.getRealname() != null) {
            SET("realName = #{realname,jdbcType=VARCHAR}");
        }
        
        if (record.getSerialnumber() != null) {
            SET("serialNumber = #{serialnumber,jdbcType=VARCHAR}");
        }
        
        if (record.getTelephone() != null) {
            SET("telephone = #{telephone,jdbcType=VARCHAR}");
        }
        
        if (record.getAddProfileId() != null) {
            SET("add_profile_id = #{addProfileId,jdbcType=BIGINT}");
        }
        
        if (record.getDeparmentId() != null) {
            SET("deparment_id = #{deparmentId,jdbcType=BIGINT}");
        }
        
        if (record.getParentSaleId() != null) {
            SET("parent_sale_id = #{parentSaleId,jdbcType=BIGINT}");
        }
        
        if (record.getTechnologyDeparmentId() != null) {
            SET("technology_deparment_id = #{technologyDeparmentId,jdbcType=BIGINT}");
        }
        
        if (record.getTechnologyProfileId() != null) {
            SET("technology_profile_id = #{technologyProfileId,jdbcType=BIGINT}");
        }
        
        if (record.getUserId() != null) {
            SET("user_id = #{userId,jdbcType=BIGINT}");
        }
        
        if (record.getIsAdmin() != null) {
            SET("is_admin = #{isAdmin,jdbcType=BIT}");
        }
        
        if (record.getProinceBrand() != null) {
            SET("proince_brand = #{proinceBrand,jdbcType=LONGVARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(UserProfilesExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}